import { getProjectPermission,findByCity } from './policyListServ.js'
// import { parse } from 'qs'
import { message } from 'antd'
// import moment from 'moment'
import { makeAddress } from "utils/util"
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import $ from 'jquery';

const defultState = {
    areaList: [],
    cityList: [],
    biz: {
        queryForm: {}
    },
}

const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) })
                    dispatch({ type: 'getAreaList', payload: {} });
                }
            })
        },
    },


    effects: {
        *getAreaList({payload}, {put, select, call}){
            let {areaList} = yield select(state => state[tmpModule.namespace])
            let params = { 
              userId: localStorage.username
            }
            const { success,projectList} = yield call(getProjectPermission, {...params})
            areaList =  projectList.map((item,index)=>{
                          return {
                            label: item.areaName,
                            value: item.areaId,
                          }
                        })
              yield put({
                type: 'updateStore',
                payload: {areaList}
              })
              if(sessionStorage.policyAreaId){
                yield put({type: 'findByCity',payload: {areaId: sessionStorage.policyAreaId}})
              }
        },
        *findByCity({payload}, {put, select, call}){
            let {cityList} = yield select(state => state[tmpModule.namespace])
            let params = { 
              area: payload.areaId,
            }
            const { success,result} = yield call(findByCity, {...params})
            if(success){
              let list = result.filter(item => !!item)
              cityList =  list.map((item,index)=>{
                          return {
                            label: item.city,
                            value: item.cityid,
                          }
                        })
              yield put({
                type: 'updateStore',
                payload: {cityList}
              })
            }
          },
    },
    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            return Object.assign({}, preState, action.payload)
        },
    }
}

export default tmpModule
